package com.chenhao.springcloud.chenhaomqservice.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Description：方法执行时间切面
 *
 * @author chenhao
 * @date 2020/6/9 14:02
 */
@Component
@Aspect
public class ServiceLogAspect {
    public static final Logger log = LoggerFactory.getLogger(ServiceLogAspect.class);

    @Around("execution(* com.chenhao.springcloud.chenhaomqservice.service.*.*(..))")
    public Object recordTimeLog(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("=========开始执行 {}.{}=============",
        joinPoint.getTarget().getClass(),
        joinPoint.getSignature().getName());
        // 记录开始时间
        Long beginTime = System.currentTimeMillis();
        // 执行目标
        Object result = joinPoint.proceed();
        // 结束时间
        Long endTime = System.currentTimeMillis();
        Long takeTime = endTime-beginTime;
        if (takeTime>3000){
            log.error("=======执行结束，耗时：{} 毫秒=======",takeTime);
        }else if (takeTime>2000){
            log.warn("=======执行结束，耗时：{} 毫秒=======",takeTime);
        }else {
            log.info("=======执行结束，耗时：{} 毫秒=======",takeTime);
        }
        return result;



    }

}
